package com.leff.cards.data;

import java.util.ArrayList;

import org.json.simple.JSONArray;

import com.leff.cards.util.Secrets;
import com.leff.cards.util.Dictionary;

public class HistoryItem implements Secrets {

	public String query;
	public String winner;
	public ArrayList<String> responses;
	public String chosenResponse;
	
	private HistoryItem() {}
	
	public static HistoryItem fromDictionary(Dictionary dict) {
		
		HistoryItem HI = new HistoryItem();
		
		Dictionary query = Dictionary.fromJSONObject(dict.get("query"));
		QueryCard QC = QueryCard.fromDictionary(query);
		HI.query = QC.getTextWithResponses();
		
		Dictionary user = Dictionary.fromJSONObject(dict.get("user"));
		User U = User.fromDictionary(user);
		HI.winner = U.displayName;
		
		HI.responses = new ArrayList<String>();
		JSONArray responseArr = (JSONArray)dict.get("cards");
		for(Object O : responseArr) {
			JSONArray response = (JSONArray)O;
			CompositeResponseCard crc = CompositeResponseCard.fromJSONArray(response);
			
			HI.responses.add(crc.getText());
		}

		long chosenIndex = (Long)dict.get("winner");
		HI.chosenResponse = HI.responses.get((int)chosenIndex);
		
		return HI;
	}
}
